// 导入Flutter Material设计库
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '双击退出示例',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        scaffoldBackgroundColor: Colors.grey[100],
      ),
      home: WillPopScopeTestRoute(),
    );
  }
}

// 定义一个StatefulWidget（有状态组件），用于实现双击返回键退出功能
class WillPopScopeTestRoute extends StatefulWidget {
  @override
  // 创建对应的状态类实例
  WillPopScopeTestRouteState createState() {
    return WillPopScopeTestRouteState();
  }
}

// 定义Widget的状态类
class WillPopScopeTestRouteState extends State<WillPopScopeTestRoute> {
  // 记录上次按下返回键的时间，初始为null
  DateTime? _lastPressedAt; //上次点击时间

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (_lastPressedAt == null ||
            DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 1)) {
          _lastPressedAt = DateTime.now();
          return false;
        }
        return true;
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text('双击退出示例'),
          elevation: 0,
          centerTitle: true,
        ),
        body: Container(
          padding: EdgeInsets.all(20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                Icons.exit_to_app,
                size: 80,
                color: Colors.blue,
              ),
              SizedBox(height: 20),
              Text(
                "双击退出提示",
                style: TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                  color: Colors.black87,
                ),
              ),
              SizedBox(height: 10),
              Text(
                "1秒内连续按两次返回键退出",
                style: TextStyle(
                  fontSize: 16,
                  color: Colors.black54,
                ),
              ),
              SizedBox(height: 30),
              Card(
                elevation: 4,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(15),
                ),
                child: Padding(
                  padding: EdgeInsets.all(20),
                  child: Column(
                    children: [
                      Text(
                        "操作说明",
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(height: 10),
                      Text(
                        "点击下方按钮或使用返回键\n在1秒内连续操作两次即可退出",
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Colors.black54,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              SizedBox(height: 30),
              ElevatedButton.icon(
                onPressed: () async {
                  if (_lastPressedAt == null ||
                      DateTime.now().difference(_lastPressedAt!) >
                          Duration(seconds: 1)) {
                    setState(() => _lastPressedAt = DateTime.now());
                  } else {
                    Navigator.of(context).pop();
                  }
                },
                icon: Icon(Icons.exit_to_app),
                label: Text("返回"),
                style: ElevatedButton.styleFrom(
                  padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}